<?php
class OrderAction extends AdminbaseAction {
	
	protected $dao;
	function _initialize() {
		parent::_initialize ();
		$this->dao = M ( 'Order' );
	
	}
	
	public function index() {
		$template = file_exists ( TEMPLATE_PATH . '/' . GROUP_NAME . '/' . MODULE_NAME . '_index.html' ) ? MODULE_NAME . '_index' : 'content_index';
		$this->_list ( MODULE_NAME );
		$this->display ( $template );
	}
	
	public function edit() {
		$sn = $_REQUEST ['sn'];
		$id = $_REQUEST ['id'];
		$model = M ( 'Order' );
		if ($_POST ['dosubmit']) {
			if (false === $model->create ()) {
				$this->error ( $model->getError () );
			}
			$orderid = $model->save ();
			
			if (false !== $orderid) {
				$this->success ( L ( 'edit_ok' ) );
			} else {
				$this->error ( $model->getDbError () );
			}
		} else {
			$cart = $id ? $model->find ( $id ) : $model->getBySn ( $sn );
			if (empty ( $cart )) {
				$this->error ( L ( 'do_empty' ) );
			}
			$p = M ( $cart ['module'] );
			$cart ['productlist'] = unserialize ( $cart ['productlist'] );
			foreach ( ( array ) $cart ['productlist'] as $key => $rs ) {
				$cart ['totalnum'] += $rs ['num'];
				$cart ['totalprice'] += $rs ['num'] * $rs ['price'];
				$cart ['productlist'] [$key] ['countprice'] = $rs ['num'] * $rs ['price'];
				$cart ['productlist'] [$key] ['info'] = $p->find ( $rs ['id'] );
			}
			$this->assign ( 'cart', $cart );
		}
		$this->display ();
	
	}
	public function show() {
		$sn = $_REQUEST ['sn'];
		$id = $_REQUEST ['id'];
		$model = M ( 'Order' );
		$cart = $id ? $model->find ( $id ) : $model->getBySn ( $sn );
		if (empty ( $cart )) {
			$this->error ( L ( 'do_empty' ) );
		}
		
		$p = M ( $cart ['module'] );
		$cart ['productlist'] = unserialize ( $cart ['productlist'] );
		
		foreach ( ( array ) $cart ['productlist'] as $key => $rs ) {
			$cart ['totalnum'] += $rs ['num'];
			$cart ['totalprice'] += $rs ['num'] * $rs ['price'];
			$cart ['productlist'] [$key] ['countprice'] = $rs ['num'] * $rs ['price'];
			$cart ['productlist'] [$key] ['info'] = $p->find ( $rs ['id'] );
		}
		$this->assign ( 'cart', $cart );
		$this->display ();
	}
	
	/**
	   *指定未处理信息订单或指定日期订单汇总
	   *@date 2011-9-22
	   *@time 上午03:14:54
	   */
	public function all_order() {
		//指定未处理信息订单或指定日期订单汇总
		
		$model = $this->dao;
		$id = $model->getPk ();
		$this->assign ( 'pkid', $id );
		$map=array();
		if (isset ( $_REQUEST ['order'] )) {
			$order = $_REQUEST ['order'];
		} else {
			$order = ! empty ( $sortBy ) ? $sortBy : $id;
		}
		if (isset ( $_REQUEST ['sort'] )) {
			$_REQUEST ['sort'] == 'asc' ? $sort = 'asc' : $sort = 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		$_REQUEST ['sort'] = $sort;
		$_REQUEST ['order'] = $order;
		if(empty($_POST['ids'])){
			$keyword = $_REQUEST ['keyword'];
			$searchtype = $_REQUEST ['searchtype'];
			$groupid = intval ( $_REQUEST ['groupid'] );
			$catid = intval ( $_REQUEST ['catid'] );
			$posid = intval ( $_REQUEST ['posid'] );
			$typeid = intval ( $_REQUEST ['typeid'] );
			$tid = intval ( $_REQUEST ['tid'] );
			$userid = intval ( $_REQUEST ['userid'] );
			$username = intval ( $_REQUEST ['username'] );
			
			if($_REQUEST['sy']&&$_REQUEST['sm']&&$_REQUEST['sd']){
				$sy=$_REQUEST['sy'];
				$sm=$_REQUEST['sm'];
				$sd=$_REQUEST['sd'];
				$st=mktime(0,0,0,$sm,$sd,$sy);
			}
			if($_REQUEST['ey']&&$_REQUEST['em']&&$_REQUEST['ed']){
				$ey=$_REQUEST['ey'];
				$em=$_REQUEST['em'];
				$ed=$_REQUEST['ed'];
				$et=mktime(23,59,59,$sm,$sd,$sy);
			}
			if(isset($st)&&isset($et)){
				$map['createtime']=array(array('gt',$st),array('lt',$et));
			}elseif(isset($st)&&empty($et)){
				$map['createtime']=array(array('gt',$st),array('lt',time()));
			}elseif(isset($et)&&empty($st)){
				$map['createtime']=array('lt',$et);
			}

			
			if (! empty ( $keyword ) && ! empty ( $searchtype )) {
				$map [$searchtype] = array ('like', '%' . $keyword . '%' );
			}
			if ($groupid)
				$map ['groupid'] = $groupid;
			if ($catid)
				$map ['catid'] = $catid;
			if ($posid)
				$map ['posid'] = $posid;
			if ($typeid)
				$map ['typeid'] = $typeid;
			if ($tid)
				$map ['tid'] = $tid;
			if ($userid)
				$map ['userid'] = $userid;
			if ($username)
				$map ['username'] = $username;
			if (isset ( $_REQUEST ['status'] ) && ($_REQUEST ['status'] === '0' || $_REQUEST ['status'] > 0)) {
				$map ['status'] = intval ( $_REQUEST ['status'] );
			} else {
				unset ( $_REQUEST ['status'] );
			}
		}else{
			$ids='';
			foreach ($_POST['ids'] as $v){
				$ids.=$v.',';
			}
			
			$map['id']=array('IN',$ids);
		}
		$this->assign ( $_REQUEST );
		$count = $model->where ( $map )->count ( $id );
		//dump($model->getlastsql());
		if ($count > 0) {
			import ( "@.ORG.Page" );
			//创建分页对象
			if (! empty ( $_REQUEST ['listRows'] )) {
				$listRows = $_REQUEST ['listRows'];
			}
			$p = new Page ( $count, $listRows );
			//分页查询数据
			

			$field = $this->module [$this->moduleid] ['listfields'];
			$field= (empty($field) || $field=='*') ? '*' : 'id,catid,url,title,title_style,userid,username,hits,createtime,status' ;
			$voList = $model->field ( $field )->where ( $map )->order ( "`" . $order . "` " . $sort )->limit ( $p->firstRow . ',' . $p->listRows )->findAll ();
			//dump($model->getlastsql());
			//分页跳转的时候保证查询条件
			foreach ( $map as $key => $val ) {
				if (! is_array ( $val )) {
					$p->parameter .= "$key=" . urlencode ( $val ) . "&";
				}
			}
			$map [C ( 'VAR_PAGE' )] = '{$page}';
			$page->urlrule = U ( $modelname . '/index', $map );
			
			//分页显示
			$page = $p->show ();
			//列表排序显示
			$sortImg = $sort; //排序图标
			$sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
			$sort = $sort == 'desc' ? 1 : 0; //排序方式
	
			$new_arr=array();
			foreach ($voList as $v){
				$v['productlist']=unserialize($v['productlist']);
				foreach ($v['productlist'] as $r){
					if(isset($new_arr[$r['id']])){
						$new_arr[$r['id']]['num']=$new_arr[$r['id']]['num']+$r['num'];
					}else{
						$new_arr[$r['id']]=$r;
					}
				}
			}
			$this->assign("list",$new_arr);
			$this->assign ( 'page', $page );
		}
		$this->display ("Order_all");
	}//end all_order
}
?>